/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Config;
import gui.ServerConfig;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 *
 * @author HungVM_C00359
 */
public class ConfigXML {
    private String serverName="";
    private String dataName="";
    private String userName="";
    private String passWord="";
    private String port;

    ServerConfig sc = null;

    public ConfigXML(ServerConfig sc) {
        this.sc = sc;
    }

    public ConfigXML() {
    }

    


    //Tao file XML
    public void createXML(){
        try{
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

	  //root elements
	  Document doc = docBuilder.newDocument();
          Element root  =doc.createElement("LoanProcessSystem");
          doc.appendChild(root);

          Element serverName1 = doc.createElement("ServerName");
          serverName1.appendChild(doc.createTextNode(sc.getServerName()));
          root.appendChild(serverName1);

          Element dataName1 = doc.createElement("DataName");
          dataName1.appendChild(doc.createTextNode(sc.getDataName()));
          root.appendChild(dataName1);

          Element userName1 = doc.createElement("UserName");
          userName1.appendChild(doc.createTextNode(sc.getUserName()));
          root.appendChild(userName1);

          Element passWord1 = doc.createElement("PassWord");
          passWord1.appendChild(doc.createTextNode(sc.getPassWord()));
          root.appendChild(passWord1);

          Element port1 = doc.createElement("Port");
          port1.appendChild(doc.createTextNode(sc.getPort()));
          root.appendChild(port1);

          TransformerFactory transformerFactory = TransformerFactory.newInstance();
	  Transformer transformer = transformerFactory.newTransformer();
	  DOMSource source = new DOMSource(doc);
	  StreamResult result =  new StreamResult(new File("src\\Config\\ConfigXML.xml"));
	  transformer.transform(source, result);

	  System.out.println("Done");
          
        }catch(ParserConfigurationException pce){
            pce.printStackTrace();
        }catch(TransformerException tfe){
            tfe.printStackTrace();
        }
    }

    //READ XML

    public void readXML(){
        try {
              File file = new File("src\\Config\\ConfigXML.xml");
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();

              org.w3c.dom.Document doc = db.parse(file);
              doc.getDocumentElement().normalize();
              System.out.println("Root element " + doc.getDocumentElement().getNodeName()); //phong kham
              NodeList nodeLst = doc.getElementsByTagName("LoanProcessSystem");
              System.out.println("Information System");


              for (int s = 0; s < nodeLst.getLength(); s++) {
                  Node fstNode = (Node) nodeLst.item(s);
                  if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

                      Element fstElmnt = (Element) fstNode;

                      NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("ServerName");
                      Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                      NodeList fstNm = fstNmElmnt.getChildNodes();
                      System.out.println("ServerName: "  + ((Node) fstNm.item(0)).getNodeValue());
                      this.serverName = ((Node) fstNm.item(0)).getNodeValue();


                      NodeList dataElmntLst = fstElmnt.getElementsByTagName("DataName");
                      Element dataElmnt = (Element) dataElmntLst.item(0);
                      NodeList data = dataElmnt.getChildNodes();
                      System.out.println("DataName: " + ((Node) data.item(0)).getNodeValue());
                      this.dataName = ((Node) data.item(0)).getNodeValue();


                      NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("UserName");
                      Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                      NodeList lstNm = lstNmElmnt.getChildNodes();
                      System.out.println("UserName: " + ((Node) lstNm.item(0)).getNodeValue());
                      this.userName = ((Node) lstNm.item(0)).getNodeValue();
                      
                      NodeList passElmntLst = fstElmnt.getElementsByTagName("PassWord");
                      Element passElmnt = (Element) passElmntLst.item(0);
                      NodeList pass= passElmnt.getChildNodes();
                      System.out.println("PassWord: " + ((Node) pass.item(0)).getNodeValue());
                      this.passWord = ((Node) pass.item(0)).getNodeValue();

                      NodeList PortElmntLst = fstElmnt.getElementsByTagName("Port");
                      Element Portlmnt = (Element) PortElmntLst.item(0);
                      NodeList Port = Portlmnt.getChildNodes();
                      System.out.println("Port: " + ((Node) Port.item(0)).getNodeValue());
                      this.port = ((Node) Port.item(0)).getNodeValue();

                }

              }
              } catch (Exception e) {
                e.printStackTrace();
              }


    }

    public String getDataName() {
        return dataName;
    }

    public void setDataName(String dataName) {
        this.dataName = dataName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}
